#!/bin/bash

set -e

# Start consumer on the non-rails application
echo 'Starting non rails application consumer...'
pushd example/non_rails_app >/dev/null
bundle >/dev/null
bundle exec rake
bundle exec rabbit_feed consume --environment development --require `pwd`/lib/non_rails_app.rb --daemon --verbose
sleep 1
popd >/dev/null
echo 'Non rails application consumer started'

# Start consumer on the rails application
echo 'Starting rails application consumer...'
pushd example/rails_app >/dev/null
bundle >/dev/null
bundle exec rake db:reset
bundle exec rake db:reset RAILS_ENV=test
bundle exec rake
bundle exec rabbit_feed consume --environment development --daemon --verbose
sleep 1
popd >/dev/null
echo 'Rails application consumer started'

# Start the rails application
echo 'Starting rails application...'
pushd example/rails_app >/dev/null
bundle exec unicorn -c config/unicorn.rb -D
sleep 1
popd >/dev/null
echo 'Rails application started'

# Trigger an event
echo 'Triggering event...'
curl -silent --data "beaver[name]=`date '+%m/%d/%y %H:%M:%S'`" http://127.0.0.1:8080/beavers >/dev/null
echo 'Event triggered'

sleep 3

# Stop consumer on the non-rails application
echo 'Stopping non rails application consumer...'
pushd example/non_rails_app >/dev/null
kill `cat tmp/pids/rabbit_feed.pid`
popd >/dev/null
echo 'Non rails application consumer stopped'

# Stop consumer on the rails application
echo 'Stopping rails application consumer...'
pushd example/rails_app >/dev/null
kill `cat tmp/pids/rabbit_feed.pid`
popd >/dev/null
echo 'Rails application consumer stopped'

# Stop the rails application
echo 'Stopping rails application...'
pushd example/rails_app >/dev/null
kill `cat tmp/pids/unicorn.pid`
popd >/dev/null
echo 'Rails application stopped'
